/**
 * created by Botme
 */

var CommonLib = (function () {

    var _that = {};

    // 获取input[type=file]文件名称
    _that.getFileInputName = function (fileId) {
        var filevalue = document.getElementById(fileId).value;
        var pos = filevalue.lastIndexOf("\\");
        return filevalue.substring(pos + 1);
    }

    // 生成随机数
    _that.randNum = function (min, max) {
        return parseInt(Math.random() * (max - min + 1) + min, 10);
    }

    // 判断数组元素是否唯一
    _that.isRepeatArray = function (arr) {
        var hash = {};
        for (var i in arr) {
            if (!arr[i]) {
                continue;
            }
            if (hash[arr[i]]) {
                return true;
            }
            hash[arr[i]] = true;
        }
        return false;
    }

    // 正则方面
    var _regExp = {};

    _regExp.noSymool = /[^a-zA-Z0-9]/g;

    // 通用匹配替换
    _regExp.replace = function (text, regExp, replacement) {
        return text.replace(regExp, replacement);
    }

    // 不允许标点符号 替换为空
    _regExp.noSymoolReplace = function (obj) {
        obj.value = _regExp.replace(obj.value, _regExp.noSymool, '');
    }

    // _regExp.noHanzi = /[\u4E00-\u9FA5\\\/\&]/g;
    _regExp.noHanzi = /[\u4E00-\u9FA5]/g;

    // 不允许中文 替换为空
    _regExp.noHanziReplace = function (obj) {
        obj.value = _regExp.replace(obj.value, _regExp.noHanzi, '');
    }

    _regExp.noNumber = /\D+/g;

    // 不允许非数字 替换为空
    _regExp.noNumberReplace = function (obj) {
        obj.value = _regExp.replace(obj.value, _regExp.noNumber, '');
    }

    _that.regExp = _regExp;

    // 请求方面
    var _Request = {};

    _Request.ajax = function (p, d, sf, m) { /*(path,data,success or fail,method)*/
        var xhr = new XMLHttpRequest();
        var hm = '';
        for (var ap in d) {
            hm = hm + ap + '=' + d[ap] + '&';
        }
        hm = hm.substring(0, hm.length - 1);
        xhr.open('post', p, true);
        if (m !== 'multipart/form-data') {
            xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
            xhr.send(hm);
        } else {
            xhr.send(d);
        }
        xhr.onreadystatechange = function () {
            if (xhr.readyState == 4 && xhr.status == 200) {
                sf.success(xhr.responseText);
            } else if (xhr.readyState == 4 && xhr.status !== 200) {
                sf.failed(xhr.status);
            }
        };
    }

    _that.ls = new Array();
    _that.script = function (url) {
        var script = document.createElement("script");
        var exist = false;
        for (var up in _that.ls) {
            if (_that.ls[up] == url) {
                exist = true;
                break;
            }
        }
        if (!exist) {
            _that.ls[_that.ls.length] = url;
            script.type = "text/javascript";
            script.src = url;
            document.body.appendChild(script);
        }
    }
    _that.Request = _Request;
    return _that;
})();